// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#include <gtest/gtest.h>
#include <pollux/dwio/common/metrics_log.h>

using namespace kumo::pollux::dwio::common;

namespace {
class TestingDwioMetricsLog : public MetricsLog {
 public:
  TestingDwioMetricsLog(const std::string& filePath, bool ioLogging)
      : MetricsLog(filePath) {
    ioLogging_ = ioLogging;
  }

  const std::string& filePath() const {
    return file_;
  }

  bool ioLogging() const {
    return ioLogging_;
  }

  void logRead(const ReadMetrics& /* unused */) const override {}

  void logColumnFilter(
      const ColumnFilter& /* unused */,
      uint64_t /* unused */,
      uint64_t /* unused */,
      bool /* unused */) const override {}

  void logWrite(
      const std::string& /* unused */,
      size_t /* unused */,
      size_t /* unused */) const override {}

  void logStripeFlush(const StripeFlushMetrics& /* unused */) const override {}

  void logFileClose(const FileCloseMetrics& /* unused */) const override {}

 private:
  bool ioLogging_;
};

class TestingDwioMetricsLogFactory : public DwioMetricsLogFactory {
  MetricsLogPtr create(const std::string& filePath, bool ioLogging) override {
    return std::make_shared<TestingDwioMetricsLog>(filePath, ioLogging);
  }
};
} // namespace

TEST(DwioMetricsLogFactoryTest, basic) {
  auto defaultLog = getMetricsLogFactory().create("path/to/data.orc", false);
  ASSERT_TRUE(defaultLog != nullptr);
  registerMetricsLogFactory(std::make_shared<TestingDwioMetricsLogFactory>());

  {
    auto customLog = getMetricsLogFactory().create("path/to/data.orc", false);
    ASSERT_TRUE(customLog != nullptr);
    auto testingLog =
        std::dynamic_pointer_cast<const TestingDwioMetricsLog>(customLog);
    ASSERT_TRUE(testingLog != nullptr);
    ASSERT_EQ("path/to/data.orc", testingLog->filePath());
    ASSERT_FALSE(testingLog->ioLogging());
  }

  {
    auto customLog = getMetricsLogFactory().create("path/to/data2.orc", true);
    ASSERT_TRUE(customLog != nullptr);
    auto testingLog =
        std::dynamic_pointer_cast<const TestingDwioMetricsLog>(customLog);
    ASSERT_TRUE(testingLog != nullptr);
    ASSERT_EQ("path/to/data2.orc", testingLog->filePath());
    ASSERT_TRUE(testingLog->ioLogging());
  }
}
